package remote.gummachine;

import remote.gummachine.state.*;

/**
 * gum ball machine
 * @author yuanml
 */
public class GumballMachine {
    private State soldState;
    private State soldOutState;
    private State noQuarterState;
    private State hasQuarterState;
    private State concurrentState;
    private int count = 0;
    public GumballMachine(int numberGumballs) {
        this.count = numberGumballs;
        soldOutState = new SoldOutState(this);
        soldState = new SoldState(this);
        noQuarterState = new NoQuarterState(this);
        hasQuarterState = new HasQuarterState(this);
        concurrentState = noQuarterState;
    }

    public void insertQuarter() {
        this.concurrentState.insertQuarter();
    }

    public void ejectQuarter() {
        this.concurrentState.ejectQuarter();
    }

    public void turnCrank() {
        this.concurrentState.turnCrank();
    }

    public void dispense() {
        this.concurrentState.dispense();
    }

    public void setState(State state) {
        this.concurrentState = state;
    }

    public void releaseBall() {
        if (count < 1) {
            System.out.println("sold out..");
            return;
        }
        System.out.println("A gumball comes rolling out the slot..");
        if (count != 0) {
            count--;
        }
    }

    public void operateTheMachine() {
        this.insertQuarter();
        this.ejectQuarter();
        this.turnCrank();
        this.dispense();
    }

    public State getSoldState() {
        return soldState;
    }

    public void setSoldState(State soldState) {
        this.soldState = soldState;
    }

    public State getSoldOutState() {
        return soldOutState;
    }

    public void setSoldOutState(State soldOutState) {
        this.soldOutState = soldOutState;
    }

    public State getNoQuarterState() {
        return noQuarterState;
    }

    public void setNoQuarterState(State noQuarterState) {
        this.noQuarterState = noQuarterState;
    }

    public State getHasQuarterState() {
        return hasQuarterState;
    }

    public void setHasQuarterState(State hasQuarterState) {
        this.hasQuarterState = hasQuarterState;
    }

    public State getConcurrentState() {
        return concurrentState;
    }

    public void setConcurrentState(State concurrentState) {
        this.concurrentState = concurrentState;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
}
